package com.example.netty.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NioClient {
    public static void main(String[] args) throws IOException {
        //直接声明socketChannel,并设置非阻塞模式
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        //声明地址和端口号
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",6666);

        //开始连接
        if (!socketChannel.connect(inetSocketAddress)) {
            //未连接成功
            while (!socketChannel.finishConnect()) {
                System.out.println("因为连接需要时间，客户端不会阻塞，可以做其他工作......");
            }
        }
        //连接成功,发送数据(wrap会根据发送内容自动自动申请buffer大小)
        ByteBuffer buffer = ByteBuffer.wrap("hello nio!".getBytes());
        socketChannel.write(buffer);
        //阻塞主线程
        System.in.read();
    }
}










